﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace OGS.Utility
{
    /// <summary>
    /// This class is a helper class for string
    /// </summary>
    public static class StringHelper
    {
        /// <summary>
        /// Convert a String to SEO friendly url
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string ToSeoUrl(this string url)
        {
            // make the url lowercase
            string encodedUrl = (url ?? "").ToLower();
            encodedUrl = encodedUrl.Replace(" ", "-");


            // replace & with and
            encodedUrl = Regex.Replace(encodedUrl, @"\&+", "and");

            // remove characters
            encodedUrl = encodedUrl.Replace("'", "");

            // remove invalid characters
            encodedUrl = Regex.Replace(encodedUrl, @"[^a-z0-9]", "-");

            // remove duplicates
            encodedUrl = Regex.Replace(encodedUrl, @"-+", "-");

            // trim leading & trailing characters
            encodedUrl = encodedUrl.Trim('-');

            return encodedUrl;
        }

    }


}
